<?php
App::import('vendor','paginator');
class LoginController extends AppController
{
	var $name            = "Login";
	var $uses            = array('Login','Page','Cron');
	var $components      = array('Session');
	var $metaDescription = "";
	var $metaKeywords    = "";
	var $layout          = "account_default";

	function beforeFilter(){
		//redirect if the user is not logged in________________________________	
    	parent::beforeFilter();
		if($this->Session->check('logged')){
			$this->redirect('/'.UC_MAP);
		}	
    }
	
	function findFriends(){
		$this->set('groups',$this->Login->getGroups());
		$this->layout = "old_game";
		$this->pageTitle = "Find your friends";	
	}
	
	function account(){	
		$this->set('cities',$this->Login->getGameCities());
		$message = "";		
		if($this->Session->check('message')){
			$this->set('login_message',$this->Session->read('message'));
			$this->Session->del('message');
		}

		if(isset($this->params['form']) && !empty($this->params['form'])){
			if(!empty($this->params['form']['login_send'])){
				$sess_info = $this->Login->checkLogin($this->params['form']);
				$message = $this->login($this->params['form']);
				if(!empty($message)){
					$this->set('login_message', $message);
				} else {
					$this->requestAction("/Cron/makeUpdates/");	
					$this->redirect('/'.UC_MAP);
				}
			} elseif (!empty($this->params['form']['create_send'])){
				$message = $this->create($this->params['form']);
				if(!empty($message)){	
					$this->set('create_message', $message);
				} else {
					unset($this->params['form']);
					$message = 'You account was successfully created. Please login!';
					$this->Session->write('message', $message);
					$this->redirect(U_HOME."#login");
				}
			} elseif (!empty($this->params['form']['recover_send'])){
				$message = $this->recover($this->params['form']);
				if(!empty($message)){	
					$this->set('recover_message', $message);
				} else {	
					unset($this->params['form']);
					$message = 'The password was successfully send. Please login!';
					$this->Session->write('message', $message);
					$this->redirect(U_HOME."#login");
				}
			}		
		}
		$this->pageTitle = 'Welcome';	
	}
	
	function create($params){
		$message = '';
		$cities = explode(", ",$this->Login->getGameCities());
		//check that fields are filled in_______________
		$required = array('email'=>'Email address', 'password' =>'Password',
						'first_name'=> 'First name', 'last_name'=>'Last name',
						'sex'=>'sex', 're_password'=>'Repeat password', 
						'game_choice' => 'Group choise');
		foreach($required as $req=>$mess){
			if(!isset($params[$req]) || empty($params[$req])) {
				$message .= '- The "<i>'.$mess.'</i>" field was not filled in<br/>';
			}
		}
		if($params['game_choice']=='old' && !$this->Session->check('pre_group_id')){
			$message .= '- You did not select a group to join!';
		}elseif($params['game_choice']=='new' && (!isset($params['game_name']) 
		|| empty($params['game_name']) || !isset($params['city_name']) 
		|| empty($params['city_name']) || 
		!in_array(ucfirst(strtolower($params['city_name'])),$cities))){
			$message .= '- You did not indicate the name of new group and/or city!';
		}
		if($params['password'] != $params['re_password']){
			$message .= '- The two passwords do not coincide';
		}	
		if(!preg_match('/^[^@]*@[^@]*\.[^@]*$/',$params['email'])) {
			$message .= '- Your email address is not valid.<br/>';
		}
		
		if(empty($message)){
			$group_id = 0;
			if($this->Session->check('pre_group_id')){	
				$group_id = $this->Session->read('pre_group_id');
			}
			$message = $this->Login->createAccount($params,$group_id);
		}
		$this->Session->del('pre_group_id');
		return $message;
	}	
	
	function recover($params){
		$message = '';
		if(!isset($params['email']) || empty($params['email'])) {
				$message .= '- The "<i>Email address</i>" field was not filled in<br/>';
			}
		if(!preg_match('/^[^@]*@[^@]*\.[^@]*$/',$params['email'])) {
			$message .= '- Your email address is not valid.<br/>';
		}
		if(empty($message)){	
			$this->Login->getPassword($params);
		} 
		return $message;
	}	
	
	function login($params){	
		$message = '';
		
		//check that fields are filled in_______________
		$required = array('email'=>'Email address', 'password' =>'Password');
		foreach($required as $req=>$mess){
			if(!isset($params[$req]) || empty($params[$req])) {
				$message .= '- The "<i>'.$mess.'</i>" field was not filled in<br/>';
			}
		}	
		if(!preg_match('/^[^@]*@[^@]*\.[^@]*$/',$params['email'])) {
			$message .= '- Your email address is not valid.<br/>';
		}
		if(empty($message)){
			$sess_info = $this->Login->checkLogin($params);
			if(count($sess_info) == 0){
				$message = 'The password/email address is incorrect!';
			} else {
				$this->Session->write('logged', $sess_info['user_id']);
				$this->Session->write('game_id', $sess_info['game_id']);
				if(!file_exists('js/swarm'.$sess_info['game_id'].'.js')) {
					$this->Cron->initializeRegions($sess_info['game_id']);	
					$this->Cron->initializeSwarm($sess_info['game_id']);
					$this->Cron->regionsWorthUpdate();
				}
			}
		}
		return $message;
	}		
	
	function showRankings() {
		$this->pageTitle = "Game Rankings";
	}
}
